Problem: When using the Sitecore back-end with the https protocol there is a problem with the RAD Editor which converts relative links to absolute links with a https prefix in Rich Text fields. There is the same problem with links to anchors.
Workaround: This problem can be fixed by writing an additional processor for the saveUI pipeline. The code replaces all the following entries <a href=”https://DOMAIN with <a href=” if the current domain is equal to the domain of the link in the Rich Text fields.
using System.Text.RegularExpressions;
using Sitecore;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.Pipelines.Save;
using Sitecore.Web;
namespace custom.https
{
public class FixHttps
{
private readonly string startPattern;
private readonly string replacement;
public FixHttps()
{
startPattern = string.Format("<a href=\"https://{0}", WebUtil.GetHostName());
replacement = "<a href=\"";
}
public void Process(SaveArgs args)
{
foreach (SaveArgs.SaveItem item in args.Items)
{
Item itemContext = Context.ContentDatabase.Items[item.ID, item.Language, item.Version];
if (itemContext != null)
{
foreach (SaveArgs.SaveField field in item.Fields)
{
Field fieldCurrent = itemContext.Fields[field.ID];
if ((fieldCurrent.Type == "html") || (fieldCurrent.Type == "rich text"))
{
field.Value = TightenHTTPSRelativeItemLinks(field.Value);
}
}
}
}
}
private string TightenHTTPSRelativeItemLinks(string html)
{
if (html != string.Empty)
{
html = Regex.Replace(html, "<a([^>]*)>", HttpsEvaluator, RegexOptions.Singleline | RegexOptions.IgnoreCase);
}
return html;
}
private string HttpsEvaluator(Match match)
{
string input = match.Value;
if (input.ToLower().StartsWith(startPattern))
{
input = input.ToLower().Replace(startPattern, replacement);
return input;
}
return match.Value;
}
}
}
Note: Please remember to insert the processor in the right place:
<saveUI>
<processor mode="on" type="Sitecore.Pipelines.Save.BeforeSaveEvent, Sitecore.Kernel" />
<processor mode="on" type="Sitecore.Pipelines.Save.ParseXml, Sitecore.Kernel" />
<processor mode="on" type="Sitecore.Pipelines.Save.ValidateFields, Sitecore.Kernel" />
<processor mode="on" type="Sitecore.Pipelines.Save.HasWritePermission, Sitecore.Kernel" />
<processor mode="on" type="Sitecore.Pipelines.Save.NewVersion, Sitecore.Kernel" />
<processor mode="on" type="custom.https.FixHttps, custom.dll " />
<processor mode="on" type="Sitecore.Pipelines.Save.TightenRelativeImageLinks, Sitecore.Kernel" />